//
//  WalletViewController.m
//  FWeChat
//
//  Created by iSolar on 2017/12/6.
//  Copyright © 2017年 NOOrganization. All rights reserved.
//

#import "WalletViewController.h"

@interface WalletViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

/**  滑动的BGView  **/
@property (nonatomic, strong) UIScrollView *scrollV;

@property (nonatomic, strong) UICollectionView *wallet_collection;

/**  零钱  **/
@property (nonatomic, strong) UILabel *moneyNumL;

/**  数据  **/
@property (nonatomic, strong) NSArray *dataArray;

/**  headerView  **/
@property (nonatomic, strong) UIView *headerView;

@end

@implementation WalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.scrollV];
    [self.scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollV.superview);
        make.bottom.equalTo(@(KBottomMargin));
    }];
    
    [self.scrollV addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.headerView.superview);
        make.height.equalTo(@150);
    }];
    
    [self.scrollV addSubview:self.wallet_collection];
    [self.wallet_collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.bottom.equalTo(self.wallet_collection.superview);
    }];
    
    _dataArray = @[@{@"title":@"腾讯服务",@"data":@[@{@"itemTitle":@"信用卡还款", @"img":@""},@{@"itemTitle":@"微粒贷借钱", @"img":@""},@{@"itemTitle":@"手机充值", @"img":@""},@{@"itemTitle":@"理财通", @"img":@""},@{@"itemTitle":@"生活缴费", @"img":@""},@{@"itemTitle":@"Q币充值", @"img":@""},@{@"itemTitle":@"城市服务", @"img":@""},@{@"itemTitle":@"腾讯服务", @"img":@""}]},@{@"title":@"显限时推广",@"data":@[@{@"itemTitle":@"摩拜单车", @"img":@""}]},@{@"title":@"第三方服务",@"data":@[@{@"itemTitle":@"", @"img":@"火车票机票"},@{@"itemTitle":@"", @"img":@""},@{@"itemTitle":@"", @"img":@""},@{@"itemTitle":@"", @"img":@""},@{@"itemTitle":@"", @"img":@""},@{@"itemTitle":@"", @"img":@""},@{@"itemTitle":@"", @"img":@""},@{@"itemTitle":@"", @"img":@""}]}];
}

- (UICollectionView *)wallet_collection {
    
    if (!_wallet_collection) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(kWidth/3, kWidth/3);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _wallet_collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _wallet_collection.delegate = self;
        _wallet_collection.dataSource = self;
        [_wallet_collection registerNib:[UINib nibWithNibName:@"WalletCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
        _wallet_collection.backgroundColor = [UIColor colorWithHexString:@"eeecf3"];
        [_wallet_collection registerNib:[UINib nibWithNibName:@"HeaderReusableView" bundle:nil] forCellWithReuseIdentifier:@"cellHeader"];
        
    }
    
    return _wallet_collection;
}



- (UIView *)headerView {
    
    if (!_headerView) {
        
        _headerView = [[UIView alloc] init];
        _headerView.backgroundColor = [UIColor colorWithHexString:@"5d646e"];
        NSArray *imageArray = @[@"收付款",@"零钱",@"银行卡"];
        for (int i = 0; i < imageArray.count; i ++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kWidth/3*i, 0, kWidth/3, H(_headerView))];
            btn.tag = i;
            [_headerView addSubview:btn];
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake((W(btn) - 45)/2, 30, 45, 45)];
            [btn addSubview:imageV];
            UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(0, YH(imageV)+10, W(btn), 20)];
            titleL.font = Font(15);
            titleL.textColor = [UIColor whiteColor];
            titleL.text = imageArray[i];
            titleL.textAlignment = NSTextAlignmentCenter;
            [btn addSubview:titleL];
            if (i == 1) {
                _moneyNumL = [[UILabel alloc] initWithFrame:CGRectMake(0, YH(titleL)+5, W(btn), 20)];
                [btn addSubview:_moneyNumL];
                _moneyNumL.font = Font(12);
                _moneyNumL.text = @"¥0.0";
                _moneyNumL.textAlignment = NSTextAlignmentCenter;
                _moneyNumL.textColor = [UIColor colorWithHexString:@"9ea2ad"];
            }
            
            [btn addTarget:self action:@selector(headerViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    
    return _headerView;
}

- (void)headerViewButtonAction:(UIButton *)btn {
    
    
    
}

#pragma mark -- UICollectionViewDelegate & UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return _dataArray.count;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray *arr = _dataArray[section];
    
    return arr.count;
}

#pragma mark - 头部或者尾部视图

- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath{
    
    //如果是头部视图 (因为这里的kind 有头部和尾部所以需要判断  默认是头部,严谨判断比较好)
    
    /*
     
     HeaderReusableView 头部的类  注意要继承自UICollectionReusableView 重要的事说第二遍。
     
     cellIdenfider  重用标识
     
     */
    
    if(kind ==UICollectionElementKindSectionHeader) {
        
        HeaderReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"cellHeader" forIndexPath:indexPath];
        
        return header;
        
    }else {
        return nil;
        
    }
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WalletCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (!cell) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
}

@end
